﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using EntreeServer.Module;
using Microsoft.SharePoint;

namespace ME.SDK.SampleApplication.ControlTemplates
{
    public partial class ListSearchAppConfigPartTemplate : UserControl
    {
        #region events
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack) return;
            bindLists();
            bindViews();
            bindFields();
        }

        protected void ddlView_SelectedIndexChanged(object sender, EventArgs e)
        {
            bindFields();
        }

        protected void ddlList_SelectedIndexChanged(object sender, EventArgs e)
        {
            bindViews();
            bindFields();
        }
        #endregion

        #region private methods
        void bindLists()
        {
            //bind lists from current web to the drop down
            this.ddlList.Items.Clear();
            this.ddlList.Items.AddRange(SPContext.Current.Web.Lists.Cast<SPList>().Where(l=>!l.Hidden).Select(l => new ListItem(l.Title, l.ID.ToString())).ToArray());
            if (this.Config.ListId == Guid.Empty && this.ddlList.Items.Count > 0) this.Config.ListId = new Guid(this.ddlList.Items[0].Value);
            //select current config value
            try
            {
                ddlList.Items.FindByValue(this.Config.ListId.ToString()).Selected = true;
            }
            catch { }
        }
        void bindViews()
        {
            //get the list using the selected value from the list drop down
            var listId = new Guid(this.ddlList.SelectedValue);
            var list = SPContext.Current.Web.Lists[listId];



            //bind views from list to drop down
            this.ddlView.Items.Clear();
            this.ddlView.Items.AddRange(list.Views.Cast<SPView>().Where(v=>!v.Hidden).Select(v => new ListItem(v.Title, v.ID.ToString())).ToArray());
            if (this.Config.ViewId != Guid.Empty)
            {
                //select current config value
                try
                {
                    this.ddlView.Items.FindByValue(this.Config.ViewId.ToString()).Selected = true;
                }
                catch {}
            }
            else if (list.DefaultView != null)
            {
                //select default view
                try
                {
                    this.ddlView.Items.FindByValue(list.DefaultView.ID.ToString()).Selected = true;
                }
                catch {}
            }
        }
        void bindFields()
        {
            //get the list and view using values from the drop down lists
            var listId = new Guid(this.ddlList.SelectedValue);
            var list = SPContext.Current.Web.Lists[listId];
            var viewId = new Guid(this.ddlView.SelectedValue);
            var view = list.Views[viewId];

            if (viewId != this.Config.ViewId) this.Config.FieldInternalNames.Clear();

            //bind the fields from the view to the list box
            this.lstFields.Items.Clear();
            this.lstFields.Items.AddRange(view.ViewFields.Cast<string>().Select(s => new ListItem(list.Fields.GetFieldByInternalName(s).Title, s)).ToArray());

            if (this.Config.FieldInternalNames.Any())
            {
                //select all the fields 
                lstFields.Items.Cast<ListItem>().ForEach(i => i.Selected = this.Config.FieldInternalNames.Contains(i.Value));
            }
        }
        #endregion

        #region public properties
        public ListSearchAppConfig Config { get; set; }
        #endregion

        #region public methods
        public void ApplyChanges()
        {
            //set values from controls to the config
            this.Config.ListId = new Guid(this.ddlList.SelectedValue);
            this.Config.ViewId = new Guid(this.ddlView.SelectedValue);
            this.Config.FieldInternalNames = new List<string>();
            this.Config.FieldInternalNames.AddRange(this.lstFields.GetSelectedIndices().Select(i => this.lstFields.Items[i].Value));
        }
        #endregion
    }
}
